<?php

$mValidation = new InputValidation();
$mFunction = new Fungsi();
$tRedirect = new Redirector("main.php?menu=12&submenu=311");
$mPerson = new CorePerson();



//VALIDASI INPUT NON DB DATA
$mValidation->SetInput($_POST[fAct]);
$mValidation->SetPattern(pAct);
$tAct = $mValidation->CheckValidation();

if ($tAct == "add") {
    $mDataPerson = new DBQueryExecute();
    $mDataPegawai = new DBQueryExecute();

    $mValidation->SetInput($_POST[isMultiForm]);
    $mValidation->SetPattern(pBool);
    $tIsMultiForm = $mValidation->CheckValidation();

    $mDataPerson->SetMultiForm($tIsMultiForm);
    $mDataPegawai->SetMultiForm($tIsMultiForm);
} else if ($tAct == "edt") {
    $tIsMultiForm = false;
} else {
    $mDataPerson = new DBQueryExecute();
    $mDataPerson->SetTableName("app_person");
}
////
//VALIDASI INPUT DB DATA
$mValidation->SetInput($_POST[fKategori]);
$mValidation->SetPattern(pKategoriUser);
$tKategori = $mValidation->CheckValidation();
////
//$tGoto= "main.php?menu=11&submenu=111";
if ($tAct) {
    if ($tAct == 'add') {
        //SETUP Nama Tabel
        $mDataPerson->SetTableName("app_person");
        $mDataPegawai->SetTableName("hrs_pegawai");
        //INSERT PERSON
        //KONVERSI TANGGAL
        $mFunction->ConvertDateToDBFormat("fTanggalLahir", "DD/MM/YYYY", $tIsMultiForm);

        //GENERATE PERSON ID
        $tPersonId = $mPerson->GeneratePersonId(1);

        //PREPARE SINGLE VAR to MULTIFORM VAR
        $mFunction->SetVariableToMultiFormVariableSupport("fPersonId", $tPersonId, true);
        $mFunction->SetVariableToMultiFormVariableSupport("fKategori", $tKategori);

        $mDataPerson->SetPrimaryKey('id_person', 'fPersonId', pUserId);
        $mDataPerson->SetField('nama_lengkap', 'fNama', pName);
        $mDataPerson->SetField('agama_id', 'fAgama', pUnsignedInt);
        $mDataPerson->SetField('kelamin_id', 'fKelamin', pUnsignedInt);
        $mDataPerson->SetField('goldarah_id', 'fGoldarah', pUnsignedInt);
        $mDataPerson->SetField('kota_lahir_id', 'fKotaLahir', pUnsignedInt);
        $mDataPerson->SetField('tanggal_lahir', 'fTanggalLahir', pDateDB);
        $mDataPerson->SetField('alamat', 'fAlamat', pAddress);
        $mDataPerson->SetField('kota_id', 'fKota', pUnsignedInt);
        $mDataPerson->SetField('telepon', 'fTelepon', pMobile);
        $mDataPerson->SetField('kategori_id', 'fKategori', pUnsignedInt);
        if (!$mDataPerson->GetError()) {
            $tResult = $mDataPerson->ExecuteQuery($tAct, false);
            if ($tResult) {
                //INSERT PEGAWAI
                echo "EXECUTE PERSON....ok";

                $mDataPegawai->SetPrimaryKey('id_pegawai', 'fId', pStringId);
                $mDataPegawai->SetField('person_id', 'fPersonId', pStringId);
                $mDataPegawai->SetField('jabatan_id', 'fJabatan', pJabatanId);
                if (!$mDataPerson->GetError()) {
                    echo "EXECUTE PEGAWAI....ok";
                    $tResult = $mDataPegawai->ExecuteQuery($tAct, false);
                    if ($tResult) {
                        $mDataPegawai->Commit();
                        $tRedirect->Redirecting();
                    }
                }
            } else {
                //INSERT PEGAWAI GAGAL
                echo "EXECUTE PERSON....failed";
                $mDataPerson->Rollback();
            }
        }
    } else if ($tAct == 'edt') {
        //Karena Terdapat 2 primary key yang diedit, maka PK NIP disimpan pada variabel temporary untuk sementara menjalankan proses app_person
        $tCurrentValueNIP = $_POST['fCurrentPKValue'];

        $mValidation->SetInput($_POST['fPersonId']);
        $mValidation->SetPattern(pUserId);
        $tPersonId = $mValidation->CheckValidation();


        $mFunction->SetVariableToMultiFormVariableSupport("fCurrentPKValue", $tPersonId, false, false);
        $mFunction->ConvertDateToDBFormat("fTanggalLahir", "DD/MM/YYYY", $tIsMultiForm);

        $mDataPerson = new DBQueryExecute();
        $mDataPerson->SetMultiForm($tIsMultiForm);
        $mDataPerson->SetTableName("app_person");

        $mDataPerson->SetPrimaryKey('id_person', 'fPersonId', pUserId);
        $mDataPerson->SetField('nama_lengkap', 'fNama', pName);
        $mDataPerson->SetField('agama_id', 'fAgama', pUnsignedInt);
        $mDataPerson->SetField('kelamin_id', 'fKelamin', pUnsignedInt);
        $mDataPerson->SetField('goldarah_id', 'fGoldarah', pUnsignedInt);
        $mDataPerson->SetField('kota_lahir_id', 'fKotaLahir', pUnsignedInt);
        $mDataPerson->SetField('tanggal_lahir', 'fTanggalLahir', pDateDB);
        $mDataPerson->SetField('alamat', 'fAlamat', pAddress);
        $mDataPerson->SetField('kota_id', 'fKota', pUnsignedInt);
        $mDataPerson->SetField('telepon', 'fTelepon', pMobile);
        if (!$mDataPerson->GetError()) {
            $tResult = $mDataPerson->ExecuteQuery($tAct, false);
            if ($tResult) {
                //UPDATE PEGAWAI PEGAWAI
                echo "EXECUTE PERSON....ok";

                //Kembalikan Nilai CurrentPKValue ke PK pegawai
                $mFunction->SetVariableToMultiFormVariableSupport("fCurrentPKValue", $tCurrentValueNIP, false, false);


                $mDataPegawai = new DBQueryExecute();
                $mDataPegawai->SetMultiForm($tIsMultiForm);


                $mDataPegawai->SetTableName("hrs_pegawai");


                //Hanya Senior Operator atau WebMaster yang dapat mengubah NIP
                if ($_SESSION['User']['Level'] == "9996" || $_SESSION['User']['Level'] == "168") {
                    $mDataPegawai->SetPrimaryKey('id_pegawai', 'fId', pStringId);
                } else {
                    $mDataPegawai->SetPrimaryKey('id_pegawai', 'fCurrentPKValue', pStringId);
                }

                $mDataPegawai->SetField('jabatan_id', 'fJabatan', pJabatanId);
                if (!$mDataPerson->GetError()) {
                    echo "EXECUTE PEGAWAI....ok";
                    $tResult = $mDataPegawai->ExecuteQuery($tAct, false);
                    if ($tResult) {
                        $mDataPegawai->Commit();
                        $tRedirect->Redirecting();
                    }
                }
            } else {
                //INSERT PEGAWAI GAGAL
                echo "EXECUTE PERSON....failed";
                $mDataPerson->Rollback();
            }
        }
    } else if ($tAct == 'del') {
        $mDataPerson->SetTargerURL("main.php?menu=12&submenu=311");

        $mDataPerson->SetPrimaryKey('id_person', 'fId', pUserId);
        $mDataPerson->SetField('id_person', 'fId', pUserId);
        if (!$mDataPerson->GetError())
            $tResult = $mDataPerson->ExecuteQuery($tAct);
    }
} else {
    if (!$tAct && $_POST['act']) {
        // ERROR: aksi tidak diketahui. kemungkinan diubah oleh user
    } else {
        // ERROR: nilai aksi tidak didefinisikan
    }
}
?>
